/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.admin.search;

import hibernate.dao.CourseDAO;
import hibernate.dao.EventDAO;
import hibernate.dao.StaffDAO;
import hibernate.dao.StudentDAO;
import hibernate.entity.Courses;
import hibernate.entity.Entities;
import hibernate.entity.Staffs;
import hibernate.entity.Students;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import ui.common.AbstractUIBean;
import util.Converter;
import util.SearchResult;

/**
 *
 * @author Neo
 */
@ManagedBean
@RequestScoped
public class AdminSearch extends AbstractUIBean {

    /**
     * Creates a new instance of AdminSearch
     */
    List<SearchResult> listSearchResult;
    CourseDAO courseDao = new CourseDAO();
    EventDAO eventDao = new EventDAO();
    StudentDAO stuDao = new StudentDAO();
    StaffDAO staffDao = new StaffDAO();
    int mode;
    String keyword = "Search";
    List<Students> listStudent;
    List<Staffs> listStaffs;
    List<Courses> listCourses;
    List< Entities> listEvent;

    public List<Students> getListStudent() {
        return listStudent;

    }

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    public List<SearchResult> getListSearchResult() {
        return listSearchResult;
    }

    public void setListSearchResult(List<SearchResult> listSearchResult) {
        this.listSearchResult = listSearchResult;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public AdminSearch() {
        listSearchResult = new ArrayList<SearchResult>();
    }

    public List<Staffs> getListStaffs() {
        return listStaffs;
    }

    public void setListStaffs(List<Staffs> listStaffs) {
        this.listStaffs = listStaffs;
    }

    /**
     * @return String
     * @see searchresult page is displayed with details that depends on the
     * keyword user enters to search
     */
    public String dosearch() {
        String category = "";
        switch (mode) {
            case 0:
                listStudent = stuDao.search(keyword);
                listStaffs = staffDao.search(keyword);
                for (Students s : listStudent) {
                    listSearchResult.add(new SearchResult(s.getAccounts().getFullName(), s.getAccounts().getEmail()
                            + s.getAccounts().getEmail(), "admin-student-detail"));
                }
                for (Staffs s : listStaffs) {
                    listSearchResult.add(new SearchResult(s.getAccounts().getFullName(), s.getAccounts().getEmail()
                            + s.getAccounts().getEmail(), "admin-staff-detail"));
                }
            case 1:
                listStudent = stuDao.search(keyword);
                for (Students s : listStudent) {
                    listSearchResult.add(new SearchResult(s.getAccounts().getFullName(), s.getAccounts().getEmail()
                            + s.getAccounts().getEmail(), "admin-student-detail"));
                }
                break;
            case 2:
                listStaffs = staffDao.search(keyword);
                for (Staffs s : listStaffs) {
                    listSearchResult.add(new SearchResult(s.getAccounts().getFullName(), s.getAccounts().getEmail()
                            + s.getAccounts().getEmail(), "admin-staff-detail"));
                }
                break;
            case 3:
                listCourses = courseDao.search(keyword);
                for (Courses c : listCourses) {
                    listSearchResult.add(new SearchResult(c.getCenters().getName(), c.getName()
                            + Converter.dateJavaToString(c.getStartDate()), "admin-course-detail"));
                }
                break;
            case 4:
                listEvent = eventDao.search(keyword);
                for (Entities e : listEvent) {
                    listSearchResult.add(new SearchResult(e.getTitle(), e.getContent()
                            + Converter.dateJavaToString(e.getStartTime()), "admin-event-detail"));
                }
                break;
        }
        return "admin-searchresult";

    }
}
